package com.jgdt.demo.utils;

import android.content.Context;
import android.util.Base64;

import com.librariy.util.Log;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileHelper {
    private static final String TAG = "FileHelper";
    public String encodeImage(String filePath) {
        try {
            Log.e(TAG, "filePath:" + filePath);
            BufferedInputStream in=new BufferedInputStream(new FileInputStream(filePath));
            byte[] buff=new byte[1024];
            int len=0;
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            while((len=in.read(buff))>0){
                out.write(buff,0,len);
            }
            String base64_image= Base64.encodeToString(out.toByteArray(),Base64.NO_WRAP);
            return base64_image;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
    public static File save(Context mContext, byte[] image){
        File file=new File(mContext.getExternalCacheDir(), "/camera2537/"+System.currentTimeMillis() + ".jpeg");
        try {
            if(!file.getParentFile().isDirectory()){
                file.getParentFile().mkdirs();
            }
            FileOutputStream out=new FileOutputStream(file);
            out.write(image);
            out.close();
            return file;
        } catch (Exception e) {
            android.util.Log.e(TAG,"保存文件失败",e);
            return null;
        }
    }
}
